package com.bw.educate.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bw.educate.common.ResponseResult;
import com.bw.educate.entity.EduCourseBase;
import com.bw.educate.entity.EduWebsiteAd;
import com.bw.educate.req.WebsiteCommon;
import com.bw.educate.service.IEduWebsiteAdService;
import com.bw.educate.util.TestImport;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 在线教育-五组
 * @since 2020-08-12
 */
@RestController
@RequestMapping("eduWebsite")
public class EduWebsiteAdController {

    @Autowired
    private IEduWebsiteAdService eduWebsiteAdService;

    @Autowired
    private TestImport testImport;


    /**
     * @Description:广告列表
     * @Author: 周明丽
     * @Date: 2020/8/15 21:55
     * @param websiteAd:
     * @return: com.bw.educate.common.ResponseResult
     **/
    @PostMapping("list/{currentPage}/{pageSize}")
    public ResponseResult list(@PathVariable("currentPage") int currentPage, @PathVariable("pageSize") int pageSize,@RequestBody WebsiteCommon websiteAd){
        ResponseResult result=ResponseResult.SUCCESS();
        try {
            PageHelper.startPage(currentPage,pageSize);
            QueryWrapper queryWrapper=new QueryWrapper();
            if(websiteAd.getName()!=null){
                queryWrapper.like("AD_NAME","%"+websiteAd.getName()+"%");
                queryWrapper.or();
                queryWrapper.like("AD_TITLE","%"+websiteAd.getName()+"%");
            }
            List<EduWebsiteAd> list=eduWebsiteAdService.list(queryWrapper);
            PageInfo pageInfo = new PageInfo(list);
            result.setResult(pageInfo);

        }catch (Exception e){

            result.setSuccess(false);
            result.setMessage("广告列表查询失败"+e.toString());
        }

        return result;

    }

    /**
     * @Description:删除广告
     * @Author: 周明丽
     * @Date: 2020/8/15 21:51
     * @param id:
     * @return: com.bw.educate.common.ResponseResult
     **/
    @GetMapping("deletePoster/{id}")
    public ResponseResult deletePoster(@PathVariable("id") Long id){

        ResponseResult result=ResponseResult.SUCCESS();

        try {

            eduWebsiteAdService.removeById(id);
            result.setMessage("删除成功");

        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("广告删除失败"+e.toString());
        }

        return result;
    }


    /**
     * @Description:根据id查询广告
     * @Author: 周明丽
     * @Date: 2020/8/15 21:53
     * @param id:
     * @return: com.bw.educate.common.ResponseResult
     **/
    @GetMapping("findPoster/{id}")
    public ResponseResult findPoster(@PathVariable("id") Long id){

        ResponseResult result=ResponseResult.SUCCESS();

        try {
            result.setResult(eduWebsiteAdService.getById(id));
            result.setMessage("查询成功");

        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("广告查询失败"+e.toString());
        }

        return result;
    }


    /**
     * @Description:广告添加
     * @Author: 周明丽
     * @Date: 2020/8/16 10:20
     * @param eduWebsiteAd:
     * @return: com.bw.educate.common.ResponseResult
     **/
    @PostMapping("addSave")
    public ResponseResult addSave(@RequestBody EduWebsiteAd eduWebsiteAd){

        ResponseResult result=ResponseResult.SUCCESS();

        try {
            if(eduWebsiteAd.getId()==null){
                eduWebsiteAd.setStatus(0);
                eduWebsiteAdService.saveOrUpdate(eduWebsiteAd);
            }else{
                eduWebsiteAdService.updateById(eduWebsiteAd);
            }
            result.setMessage("保存成功");

        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("广告保存失败"+e.toString());
        }

        return result;
    }


    /**
     * @Description:广告图片上传
     * @Author: 周明丽
     * @Date: 2020/8/15 21:57
     * @param file:
     * @return: com.bw.educate.common.ResponseResult
     **/
    @PostMapping("upload")
    public ResponseResult upload(MultipartFile file){

        ResponseResult result=ResponseResult.SUCCESS();
        try {
            Map map = testImport.upload(file);
            result.setResult(map);
            result.setMessage("上传成功");
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("学员头像上传失败"+e.toString());
        }
        return result;
    }

    /**
     * @Description:广告图片下载
     * @Author: 周明丽
     * @Date: 2020/8/15 21:58
     * @param path:
     * @param response:
     * @return: void
     **/
    @GetMapping("download/{path}")
    public void download(@PathVariable("path") String path, HttpServletResponse response){

        try {

            testImport.downLoad(path,response);

        }catch (Exception e){
            e.printStackTrace();
        }

    }

    @GetMapping("getList")
    public List<EduWebsiteAd> getList(){
        List<EduWebsiteAd> list=eduWebsiteAdService.list();

        return list;

    }


}
